package com.asimplefox.controllers;

import com.asimplefox.entity.Carousel;
import com.asimplefox.entity.vo.CarouselVo;
import com.asimplefox.service.ICarouselService;
import com.asimplefox.service.abst.ResultU;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能: 实现首页图片的增、删、改、查。 <br>
 * 接口设计:<br>
 * o GET /api/carousel: 获取所有轮播图。<br>
 * o POST /api/carousel: 添加新的轮播图，请求体需包含图片地址、跳转链接和排序。<br>
 * o PUT /api/carousel/:id: 更新指定ID的轮播图，需提供新的图片地址、跳转链接和排序。<br>
 * o DELETE /api/carousel/:id: 删除指定ID的轮播图。<br>
 *
 * @author github.kloping
 */
@RestController
@RequestMapping("/api/carousel")
@Slf4j
public class CarouselController implements ResultU {

    @Autowired
    ICarouselService carouselService;

    @GetMapping
    public Object get() {
        List<Carousel> list = carouselService.lambdaQuery().list();
        return (list == null || list.isEmpty()) ? EMPTY_ARRAY : list;
    }

    @PreAuthorize("hasAuthority('admin')")
    @PostMapping
    public Object add(CarouselVo carousel) {
        log.info("add carousel:{}", carousel);
        return carouselService.saveCarousel(carousel);
    }

    @PreAuthorize("hasAnyAuthority('admin','system')")
    @PutMapping
    public Object put(@RequestParam("id") Integer id, CarouselVo carouselVo) {
        log.info("update carousel-id{}:{}", id, carouselVo);
        return carouselService.updateOne(id, carouselVo);
    }

    @PreAuthorize("hasAnyAuthority('admin','system')")
    @DeleteMapping
    public Object del(@RequestParam("id") Integer id) {
        log.info("del carousel-id:{}", id);
        return carouselService.removeById(id);
    }

}
